# python -m pip install pyserial

import serial
import time

# 顶尖电子秤协议

ser = serial.Serial("COM1", 9600, timeout=1)
ser.flushInput()


def main():
    while True:
        count = ser.inWaiting()
        if count != 0:
            recv = ser.read(ser.in_waiting).decode("utf-8")
            checktime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

            if len(recv) != 16:
                continue

            print(checktime, " ---  recv --> ", dingjianOs2(recv))
            time.sleep(0.1)


def dingjianOs2(indingjiangres):
    return indingjiangres[4:12]


if __name__ == '__main__':
    main()
